//
//  ProductNew.m
//  Trading365
//
//  Created by Jason on 12-3-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "QuotationAdd.h"
#import "QuotProdAddListCell.h"
#import "QuotEditCell.h"
#import "InfoType.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "InfoImageCell.h"
#import "InfoSelectList.h"
#import "GUID.h"
#import "DHCUtil.h"
#import "QuotSelectList.h"
#import "QuotMultSelectList.h"
#import "QuotProdEdit.h"
#import "DHCUtil.h"


@implementation QuotationAdd
@synthesize table;
@synthesize InfoListTitle,InfoListName;
//@synthesize InfoNewDict;
@synthesize parentID,guid;
@synthesize InfoListTitle0,InfoListTitle1,InfoSectionTitle;
//@synthesize InfoListTitleProduct;
@synthesize sectionTotal,sectionTotalBak;
@synthesize sectionImgName;

static NSMutableDictionary *InfoNewDict;
static NSMutableDictionary *InfoSaveDict;
static NSMutableArray *InfoListTitleProduct;

static CGFloat theKeyboardHeight = 216.0f;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title=INFO_STRING_QUOTATION_ADD;
    self.table.backgroundColor=BACKGROUND_COLOR;

    NSArray *title_array = [[NSArray alloc] initWithObjects:@"报价单编号",@"客户",@"联系人",@"商机名称",@"报价日期",@"截止日期",@"成本模型",nil];
    self.InfoListTitle0 = title_array; 
    [title_array release];
    
    NSArray *title1_array = [[NSArray alloc] initWithObjects:@"价格条款",@"币种",@"起运港",@"目的港",nil];
    self.InfoListTitle1 = title1_array; 
    [title1_array release];
    
    NSArray *section_array = [[NSArray alloc] initWithObjects:@"基本信息",@"报价信息",@"报价商品",nil];
    self.InfoSectionTitle= section_array; 
    [section_array release];
    
    NSArray *name_title = [[NSArray alloc] initWithObjects:@"",nil];
    self.InfoListName = name_title; 
    [name_title release];
    
    InfoListTitleProduct = [[NSMutableArray alloc] initWithCapacity:0];
    [InfoListTitleProduct addObject:@"添加商品"];
//    [InfoListTitleProduct addObject:@"金额"];
    
    //button for svave
    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc]
                                initWithTitle:@"保存"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(save)];

    self.navigationItem.rightBarButtonItem = saveBtn;
    [saveBtn release];

    NSNumber *anumber0 = [NSNumber numberWithInteger:[self.InfoListTitle0 count]];
    NSNumber *anumber1 = [NSNumber numberWithInteger:[self.InfoListTitle1 count]];
    NSNumber *anumber2 = [NSNumber numberWithInteger:[InfoListTitleProduct count]];
    
    sectionTotal = [[NSMutableArray alloc] init];
    [sectionTotal addObject:anumber0];
    [sectionTotal addObject:anumber1];
    [sectionTotal addObject:anumber2];
    sectionTotalBak = [[NSMutableArray alloc]initWithArray:self.sectionTotal];
    
    sectionImgName = [[NSMutableArray alloc] init];
    [sectionImgName addObject:PIC_INFO_QUOT_UP];
    [sectionImgName addObject:PIC_INFO_QUOT_UP];
    [sectionImgName addObject:PIC_INFO_QUOT_UP];

    InfoNewDict = [[NSMutableDictionary alloc] initWithCapacity:0];
    InfoSaveDict = [[NSMutableDictionary alloc] initWithCapacity:0];
    
    [InfoSaveDict setValue:@"" forKey:@"0,0"];//报价单号
    [InfoSaveDict setValue:@"" forKey:@"0,1"];
    [InfoSaveDict setValue:@"" forKey:@"0,2"];
    [InfoSaveDict setValue:@"" forKey:@"0,3"];//商机
    [InfoSaveDict setValue:[DHCUtil getCurrentDate] forKey:@"0,4"];
    [InfoSaveDict setValue:[DHCUtil getCurrentDate] forKey:@"0,5"];
    [InfoSaveDict setValue:@"" forKey:@"0,6"];//成本模型
    
    [InfoSaveDict setValue:@"" forKey:@"1,0"];
    [InfoSaveDict setValue:@"" forKey:@"1,1"];
    [InfoSaveDict setValue:@"" forKey:@"1,2"];
    [InfoSaveDict setValue:@"" forKey:@"1,3"];
    [InfoNewDict setValuesForKeysWithDictionary:InfoSaveDict];
    
    //isChance
    if ([[Session share] isChance] == NO){
        [InfoNewDict setValue:@"未启用商机" forKey:@"0,3"];
        [InfoSaveDict setValue:@"0" forKey:@"0,3"];
    }
      
    keyboardHeight = theKeyboardHeight - 49;
    tableFrame=self.table.frame;

    [self refreshData];
    [self.table reloadData];

}

- (void)viewDidUnload
{
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [InfoNewDict release];
    [InfoSaveDict release];
    [parentID release];
    [guid release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ///////////////////////////
    NSNumber *anum = [NSNumber numberWithInteger:[InfoListTitleProduct count]];
    [sectionTotal replaceObjectAtIndex:2 withObject:anum];
    [sectionTotalBak replaceObjectAtIndex:2 withObject:anum];
                
    [self.table reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [sectionImgName release];
    [sectionTotal release];
    [sectionTotalBak release];
//    [InfoListTitleProduct release];
    [InfoSectionTitle release];
    [InfoListTitle0 release];
    [InfoListTitle1 release];

    InfoSaveDict = nil;
    
    [table release];
    [super dealloc];    
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2 && indexPath.row>0)
        return 75;
    else
        return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.InfoSectionTitle objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSNumber * ret=[self.sectionTotal objectAtIndex:section];
    return [ret integerValue];    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
    
    static NSString *CellIdentifier = @"QuotEditCell";
    
    QuotEditCell* cell = (QuotEditCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"QuotEditCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        
    }
    
    [cell initEditBeginBlcok:^(NSString *txtValue,int section1,int row1)
    {
        NSString *tmp_v=[NSString stringWithString:txtValue];
        NSString *tmp_k=[NSString stringWithFormat:@"%d,%d",section1,row1];
        [InfoNewDict setValue:tmp_v forKey:tmp_k];
        [InfoSaveDict setValue:tmp_v forKey:tmp_k];
//        NSLog(@"InfoNewDict1:%@",InfoNewDict1);
    }];
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    cell.accessoryType = UITableViewCellAccessoryNone;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.row = row;
    cell.section = section;
    //allow edit
    cell.textValue.userInteractionEnabled=YES;
    cell.textValue.delegate = self;
    cell.textValue.tag = row;
        
    if (section == 0 ){
        cell.labName.text = [self.InfoListTitle0 objectAtIndex:row];
        if (row == 1 || row == 2 || row == 3  || row == 6){
            cell.textValue.userInteractionEnabled=NO;
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;
        }
        
        //未启用商机，返回
        if ([[Session share] isChance] == NO && row == 3) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }

        if ([InfoNewDict count]>0){
            cell.textValue.text = [InfoNewDict objectForKey:[NSString stringWithFormat:@"%d,%d",section,row]];
        }
    }
    
    if (section == 1 ){
        cell.labName.text = [self.InfoListTitle1 objectAtIndex:row];
        cell.textValue.userInteractionEnabled=NO;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        
        if ([InfoNewDict count]>0){
            cell.textValue.text = [InfoNewDict objectForKey:[NSString stringWithFormat:@"%d,%d",section,row]];
        } 
    }
    
    if (section == 2 ){
        cell.textValue.userInteractionEnabled=NO;
        if (row == 0 ){
            if ([InfoListTitleProduct count] > row){
                cell.labName.text = [InfoListTitleProduct objectAtIndex:row];
            }
            if ([InfoListTitleProduct count] > 1){
                cell.textValue.text = @"(滑动可删除)";
            }
            
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        }
        //具体的添加的商品清单
        else{
            static NSString *CellIdentifier = @"QuotProdAddListCell";
            QuotProdAddListCell* cell = (QuotProdAddListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if(cell == nil){
                NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"QuotProdAddListCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            
            cell.contentView.backgroundColor = [UIColor clearColor];
            cell.backgroundColor = [UIColor clearColor];
            cell.backgroundView.backgroundColor = [UIColor clearColor];
            cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
            
            NSMutableDictionary *dict=nil;
            if ([InfoListTitleProduct count] > row){
                dict=[InfoListTitleProduct objectAtIndex:row];
            }
            cell.name.text=[dict objectForKey:@"product_name_ch"];
            
            NSString *tmp_v1=[dict objectForKey:@"quote_quantity"];
            NSString *tmp_v2=[dict objectForKey:@"quote_unit"];
            if (tmp_v1 == nil)
                tmp_v1=@"0";
            if (tmp_v2 == nil)
                tmp_v2=@"";
            NSString *tmp=[NSString stringWithFormat:@"%@ %@",tmp_v1,tmp_v2];
            cell.value2.text=tmp;
                        
            tmp_v1=[dict objectForKey:@"profit_rate"];
            if (tmp_v1 == nil)
                tmp_v2=@"0";
            else{
                tmp_v2=[NSString stringWithFormat:@"%@%%",tmp_v1];
            }
            cell.value1.text=[NSString stringWithFormat:@"%@",tmp_v2];
            
            tmp_v1=[dict objectForKey:@"purchase_unit_price"];
            if (tmp_v1 == nil)
                tmp_v1=@"0";
            cell.value4.text=tmp_v1;
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            return cell;
        }
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section=indexPath.section;
    NSInteger row=indexPath.row;

    if (section == 2 && row == 0){       
        QuotMultSelectList *selectList = [[QuotMultSelectList alloc] init];

        [selectList SetInfoType:INFO_TYPE_PRODUCT];
        
        [selectList initMultSelectBeginBlcok:^(NSArray *retArray){
            if ([retArray count]>0)
            {   
                for (NSDictionary *dict in retArray){
                    [InfoListTitleProduct addObject:dict];
                }
            }
        }];
        
        [self.navigationController pushViewController:selectList animated:YES];
//        [tmp release];
        [selectList release];
        
        return;
    }
    
    QuotSelectList *selectList = [[QuotSelectList alloc] init];
    
    selectList.section=section;
    selectList.row=row;
    
    //Section 0
    if (section == 0 )
    {
        //@"客户",
        if (row == 1){
            [selectList SetInfoType:INFO_TYPE_CUSTOMER];   
        }
        //联系人
        if (row == 2){
            NSString *custID=[NSString stringWithFormat:@"%@",[InfoSaveDict objectForKey:@"0,1"]];
            if ([custID length] < 1){
                UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请先选择客户." delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertview show];
                [alertview release];
                [selectList release];
                return;
            }
            [selectList setCustomerID:custID];
            [selectList SetInfoType:INFO_TYPE_CONTACT];   
        }
        //商机
        if (row == 3){            
            [selectList SetInfoType:INFO_TYPE_CHANCE];
        }
        //@"成本模型",
        if (row == 6){
            [selectList SetInfoType:INFO_TYPE_COST_MODEL];   
        }

        [selectList setLastSelectID:[InfoSaveDict objectForKey:[NSString stringWithFormat:@"0,%d",row]]];

        [selectList initSelectBeginBlcok:^(NSString *name, NSString *code,NSString *key){
            [InfoNewDict setValue:name forKey:key];
//        NSLog(@"name=%@;code=%@,key=%@",name,code,key);
            [InfoSaveDict setValue:code forKey:key];
        }];
    }

    //section 1
    if (section == 1)
    {
        //@"价格条款",
        if (row == 0){
            [selectList SetInfoType:INFO_TYPE_PRICE_TERMS];   
        }
        //@"币种",
        if (row == 1){
            [selectList SetInfoType:INFO_TYPE_CURRENCY_TYPE];   
        }
        //@"起运港",
        if (row == 2){
            [selectList SetInfoType:INFO_TYPE_DEPARTURE_PORT];   
        }
        //@"目的港",
        if (row == 3){
            [selectList SetInfoType:INFO_TYPE_ARRIVAL_PORT];   
        }
        
        [selectList setLastSelectID:[InfoSaveDict objectForKey:[NSString stringWithFormat:@"%d,%d",section, row]]];

        [selectList initSelectBeginBlcok:^(NSString *name, NSString *code,NSString *key){
            [InfoNewDict setValue:name forKey:key];
            [InfoSaveDict setValue:code forKey:key];
        }];
    }
        
    [self.navigationController pushViewController:selectList animated:YES];
    [selectList release];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2 && indexPath.row > 0)
    {
        return YES;
    }
    return NO;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    if (indexPath.section == 2 && row > 0){
        [InfoListTitleProduct removeObjectAtIndex:row];
        
        [sectionTotal replaceObjectAtIndex:2 withObject:[NSNumber numberWithInteger:[InfoListTitleProduct count]]];
        [sectionTotalBak replaceObjectAtIndex:2 withObject:[NSNumber numberWithInteger:[InfoListTitleProduct count]]];

        [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                     withRowAnimation:UITableViewRowAnimationFade];
        
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.  
    NSUInteger row=indexPath.row;
    NSUInteger section=indexPath.section;
      
    //Section 0
    if (section == 0 )
    {
        QuotSelectList *selectList = [[QuotSelectList alloc] init];
        
        selectList.section=section;
        selectList.row=row;
        
        //@"客户",
        if (row == 1){
            [selectList SetInfoType:INFO_TYPE_CUSTOMER];   
        }
        //联系人
        if (row == 2){
            NSString *custID=[NSString stringWithFormat:@"%@",[InfoSaveDict objectForKey:@"0,1"]];
            if ([custID length] < 1){
                UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请先选择客户." delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertview show];
                [alertview release];
                [selectList release];
                return;
            }
            [selectList setCustomerID:custID];
            [selectList SetInfoType:INFO_TYPE_CONTACT];   
        }
        //商机
        if (row == 3){            
            [selectList SetInfoType:INFO_TYPE_CHANCE];
        }
        //@"成本模型",
        if (row == 6){
            [selectList SetInfoType:INFO_TYPE_COST_MODEL];   
        }
      
        [selectList setLastSelectID:[InfoSaveDict objectForKey:[NSString stringWithFormat:@"0,%d",row]]];
        
        [selectList initSelectBeginBlcok:^(NSString *name, NSString *code,NSString *key){
            [InfoNewDict setValue:name forKey:key];
            //        NSLog(@"name=%@;code=%@,key=%@",name,code,key);
            [InfoSaveDict setValue:code forKey:key];
        }];
        
        [self.navigationController pushViewController:selectList animated:YES];
        [selectList release];
        return;
    }
    
    //section 1
    if (section == 1)
    {
        QuotSelectList *selectList = [[QuotSelectList alloc] init];
        
        selectList.section=section;
        selectList.row=row;
        
        //@"价格条款",
        if (row == 0){
            [selectList SetInfoType:INFO_TYPE_PRICE_TERMS];   
        }
        //@"币种",
        if (row == 1){
            [selectList SetInfoType:INFO_TYPE_CURRENCY_TYPE];   
        }
        //@"起运港",
        if (row == 2){
            [selectList SetInfoType:INFO_TYPE_DEPARTURE_PORT];   
        }
        //@"目的港",
        if (row == 3){
            [selectList SetInfoType:INFO_TYPE_ARRIVAL_PORT];   
        }
        
        [selectList setLastSelectID:[InfoSaveDict objectForKey:[NSString stringWithFormat:@"%d,%d",section, row]]];
        
        [selectList initSelectBeginBlcok:^(NSString *name, NSString *code,NSString *key){
            [InfoNewDict setValue:name forKey:key];
            [InfoSaveDict setValue:code forKey:key];
        }];
        
        [self.navigationController pushViewController:selectList animated:YES];
        [selectList release];
        return;
    }
    
    if (section == 2 && row == 0){       
        QuotMultSelectList *selectList = [[QuotMultSelectList alloc] init];
        
        [selectList SetInfoType:INFO_TYPE_PRODUCT];
        
        [selectList initMultSelectBeginBlcok:^(NSArray *retArray){
            if ([retArray count]>0)
            {   
                for (NSDictionary *dict in retArray){
                    [InfoListTitleProduct addObject:dict];
                }
            }
        }];
        
        [self.navigationController pushViewController:selectList animated:YES];
        //        [tmp release];
        [selectList release];
        
        return;
    }

    if (section == 2 && row > 0)
    {
        QuotProdEdit *prodEdit = [[QuotProdEdit alloc] init];
        prodEdit.sRow=row;
        prodEdit.sSection=section;
//        NSLog(@"InfoListTitleProduct=%@",[InfoListTitleProduct objectAtIndex:row]);
        if ([InfoListTitleProduct count] > row){
            [prodEdit setProdDetail:[InfoListTitleProduct objectAtIndex:row]];
        }else{
            [prodEdit setProdDetail:nil];
        }
//        NSLog(@"[InfoListTitleProduct objectAtIndex:%d]=%@",row,[InfoListTitleProduct objectAtIndex:row]);
        
        [prodEdit initQuotProdEditBlcok:^(NSMutableDictionary *retDict,int sIndex) {
            if ([retDict count] > 0){
                [InfoListTitleProduct replaceObjectAtIndex:sIndex withObject:retDict];
//                NSLog(@"InfoListTitleProduct=%@",InfoListTitleProduct);
            }
        }];
           
        [self.navigationController pushViewController:prodEdit animated:YES];
        [prodEdit release];
        
        return;
    }
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && (indexPath.row == 6 || indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3))
        return indexPath;
    
    if (indexPath.section == 1)
        return indexPath;
    
    if (indexPath.section == 2)
        return indexPath;
    
    return nil;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //assumes your tableview is 320 wide, makes a section header 80 pixels high
    UIView *customView = [[[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 320.0, 44.0)] autorelease];
    
    UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(4.0, 0.0, 312.0, 42.0)];
    NSString *tmp_title=[self.InfoSectionTitle objectAtIndex: section];
//    int countProd=[InfoListTitleProduct count]-1;
//    if (countProd > 0 && section == 2){
//        tmp_title=[NSString stringWithFormat:@"%@ %d",[self.InfoSectionTitle objectAtIndex: section],countProd];
//    }
    [btn setTitle:tmp_title forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    //UIImage *bg = [[UIImage imageNamed:PIC_INFO_QUOT_SECT_BG] resizableImageWithCapInsets://UIEdgeInsetsMake(20, 20, 80, 24)];
    UIImage *bg = [UIImage imageNamed:PIC_INFO_QUOT_SECT_BG];
    [btn setBackgroundImage:bg forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:[self.sectionImgName objectAtIndex:section]] forState:UIControlStateNormal];    
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 270, 0, 0);
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(sectionTap:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag=section;
    
    [customView addSubview: btn];
    [btn release];
    
    return customView;   
}


#pragma mark - text delegate
//该方法为点击输入文本框要开始输入是调用的代理方法：就是把view上移到能看见文本框的地方
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.40]; 
    self.table.frame = CGRectMake(tableFrame.origin.x,tableFrame.origin.y,tableFrame.size.width,tableFrame.size.height - 216 - 49-30);

    [self.table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:textField.tag inSection:0]
                      atScrollPosition:UITableViewScrollPositionBottom
                              animated:YES];  
    
    [UIView commitAnimations];
    
}

//该方法为完成输入后要调用的代理方法：虚拟键盘隐藏后，要恢复到之前的文本框地方
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.40]; 
    CGRect rect = tableFrame;
    rect.size.height -= 85;
    self.table.frame = rect;
    [UIView commitAnimations];
}

#pragma mark - 
#pragma mark - my func
-(void)sectionTap:(id)sender
{
    NSInteger initCount=0;
    UIButton* btn = sender;
    NSInteger tag=btn.tag;
        
    NSNumber *num=[NSNumber numberWithInteger:initCount];
    
    if (tag == 2)
        [sectionTotalBak replaceObjectAtIndex:tag withObject:[NSNumber numberWithInteger:[InfoListTitleProduct count]]];
    NSNumber *num2=[sectionTotalBak objectAtIndex:tag];
    NSNumber *num3=[sectionTotal objectAtIndex:tag];
    
    if ([num3 intValue] == initCount){
        [sectionTotal replaceObjectAtIndex:tag withObject:num2];
        [self.sectionImgName replaceObjectAtIndex:tag withObject:PIC_INFO_QUOT_UP];
    }
    else{
        [sectionTotal replaceObjectAtIndex:tag withObject:num];
        [self.sectionImgName replaceObjectAtIndex:tag withObject:PIC_INFO_QUOT_DOWN];
    }
    
    [self.table reloadData];
}


-(void) OnPreExcute
{
//    [super OnPreExcute];
}

//Jason需要重写
-(void) DoInBackground
{
    [self.table reloadData];
}

-(void) OnPostExcute
{
//    [super OnPostExcute];
}

//Jason需要重写
-(void)refreshData
{    
//    [[[TaskManager Share] GetTaskByType:NewProduct Delegate:self] Excute];
}


//Jason需要重写
-(void)save{

//    NSLog(@"InfoNewDict=%@",InfoNewDict);
//    NSLog(@"InfoSaveDict=%@",InfoSaveDict);
    NSString *alert=@"";

    if ([[InfoSaveDict objectForKey:@"1,3"] length] < 1)
        alert=@"请选择目的港.";
    
    if ([[InfoSaveDict objectForKey:@"1,2"] length] < 1)
        alert=@"请选择起运港.";

    if ([[InfoSaveDict objectForKey:@"1,1"] length] < 1)
        alert=@"请选择价格条款.";

    if ([[InfoSaveDict objectForKey:@"1,0"] length] < 1)
        alert=@"请选择币种.";

    if ([[InfoSaveDict objectForKey:@"0,6"] length] < 1)
        alert=@"请选择成本模型.";
    
    if ([[InfoSaveDict objectForKey:@"0,5"] length] < 1)
        alert=@"请输入截止日期.";

    if ([[InfoSaveDict objectForKey:@"0,2"] length] < 1)
        alert=@"请选择联系人.";
    
    if ([[InfoSaveDict objectForKey:@"0,0"] length] < 1)
        alert=@"请选择客户.";
    

    if ([alert length]>0){
        UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:alert delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
        [alertview release];
        return;
    }    
        
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableDictionary *tmp;
//    NSMutableArray *prot_product=[[NSMutableArray alloc] initWithCapacity:0];
    NSString *prot_id=[GUID getGUIDString];
    tmp = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
           prot_id,@"id",
           [InfoSaveDict objectForKey:@"0,0"],@"price_quote_no",//手动生成
           [InfoSaveDict objectForKey:@"0,1"],@"customer_id",
           [[Session share] getMemberID],@"employee_id",
           [InfoSaveDict objectForKey:@"0,2"],@"contact_id",
           [InfoSaveDict objectForKey:@"0,3"],@"chance_id",
           [InfoSaveDict objectForKey:@"0,4"],@"quote_date",
           [InfoSaveDict objectForKey:@"0,5"],@"quote_validity_date",
           [InfoSaveDict objectForKey:@"0,6"],@"cost_model_id",
           @"1",@"quote_version",
           @"01",@"quote_kind",
           @"01",@"quote_status",
           [InfoSaveDict objectForKey:@"1,0"],@"price_terms",
           [InfoSaveDict objectForKey:@"1,1"],@"currency_type",
           [InfoSaveDict objectForKey:@"1,2"],@"departure_port",
           [InfoSaveDict objectForKey:@"1,3"],@"arrival_port",
           @"QuoteMethod01",@"quote_method",
           @"0.0",@"assurance_factor",
           @"0.0",@"assurance_rate",
           @"0.0",@"freight_usd",
           @"0.0",@"rate2rmb",//对人民币汇率
           @"0.0",@"rate2usd",//对美元汇率'
           @"0.0",@"rate_usd2rmb",//美元对人民币汇率
           [DHCUtil getCurrentDateTime],@"operate_time",//美元对人民币汇率
           @"",@"organization_id",nil];
        
    NSLog(@"tmp=%@",tmp);
    //InsertQuotationBasic
    [xml InsertQuotationBasic:tmp];
    
    
    NSMutableArray *tmp_InfoListTitleProduct=[[NSMutableArray alloc] initWithArray:InfoListTitleProduct];
    [tmp_InfoListTitleProduct removeObjectAtIndex:0];
    
    for (NSMutableDictionary *dict in tmp_InfoListTitleProduct){
        NSMutableDictionary *tmp_product;
        NSString *value0=[dict objectForKey:@"quote_quantity"];
        if (value0 == nil)
            value0=@"0";
        NSString *value1=[dict objectForKey:@"quote_unit_id"];
        if (value1 == nil){
            value1=[dict objectForKey:@"basic_unit"];
        }
        NSString *value2=[dict objectForKey:@"profit_rate"];
        if (value2 == nil)
            value2=@"0";

        tmp_product = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
           [GUID getGUIDString],@"id",
           prot_id,@"price_quote_id",
           [dict objectForKey:@"product_id"],@"product_id",
           [dict objectForKey:@"purchase_unit_price"],@"purch_unit_price",
           [dict objectForKey:@"product_vat"],@"product_vat",//增值税率
           [dict objectForKey:@"product_bt"],@"product_bt",//退税率
           [dict objectForKey:@"basic_unit"],@"basic_unit",
            @"CommissionMethod03",@"commission_method",//佣金方法       
           value0,@"quote_quantity",
           value1,@"quote_unit",
           value2,@"profit_rate",nil];
        
        NSLog(@"tmp_product=%@",tmp_product);
//        [prot_product addObject:tmp_product];
        [xml InsertQuotationProduct:tmp_product];  
        
        [tmp_product release];
    }
    
//    NSLog(@"prot_product=%@",prot_product);
  
    [tmp_InfoListTitleProduct release];
    [tmp release];
    [xml release];
    
    [myToast showWithText:@"Save OK."];
    self.navigationItem.rightBarButtonItem.enabled=NO;
    
//    [[InfoType share] SetFlagFreshView:INFO_TYPE_VIEW_REFRESH];

}
@end
